package com.starinside.wp.service.impl;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.starinside.wp.foundation.vo.BindedBabyVO;
import com.starinside.wp.repository.BabyNativeRepository;
import com.starinside.wp.service.BindedBabyService;
import com.starinside.wp.utils.RemoteCallUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BindedBabyServiceImpl implements BindedBabyService {
    @Autowired
    BabyNativeRepository babyNativeRepository;

    @Value("${sso.server.url}")
    String ssoURL;

    private static final String GET_AREA_LIST_URI = "/api/sso/other_system/get_areas_by_ids";

    @Override
    public List<BindedBabyVO> getAllBindedBaby(String[] ids) {
        List<BindedBabyVO>  babies = babyNativeRepository.getAllBindedBaby(ids);
        fillAreaForBabies(babies);
        return babies;
    }

    @Override
    public List<BindedBabyVO> getAllBindedBaby(String mobile) {
        List<BindedBabyVO>  babies = babyNativeRepository.getAllBindedBaby(mobile);
        fillAreaForBabies(babies);
        return babies;
    }


    @Override
    public String[] getAllBindedBabyIds(String mobile){
        List<BindedBabyVO> babies = getAllBindedBaby(mobile);

        String[] babyIds = babies.stream().map(k -> {
            return k.getId();
        }).toArray(String[]::new);

        return babyIds;
    }

    @Override
    public String[] getAllBindedBabyIdsByParentId(String parentId){
        List<BindedBabyVO> babies = getAllBindedBabyByParent(parentId);

        String[] babyIds = babies.stream().map(k -> {
            return k.getId();
        }).toArray(String[]::new);

        return babyIds;
    }

    @Override
    public List<BindedBabyVO> getAllBindedBabyByParent(String parentId) {
        List<BindedBabyVO>  babies = babyNativeRepository.getAllBindedBabyByParent(parentId);
        fillAreaForBabies(babies);
        return babies;
    }

    @Override
    public BindedBabyVO getPreBindedBaby(String mobile, String babyId) {
        return babyNativeRepository.getPreBindedBaby(mobile, babyId);
    }

    private List<BindedBabyVO> fillAreaForBabies(List<BindedBabyVO> babies){
        String[] ids = babies.stream().map(k -> {
            return k==null ? "" : k.getAreaId();
        }).filter(k->StringUtils.isNotEmpty(k)).toArray(String[]::new);

        String temp = Lists.newArrayList(ids).toString();
        String id = temp.substring(1, temp.length() - 1);

        List<HashMap> hashMaps = null;
        if (ids == null || ids.length == 0){
            return babies;
        }else{
            hashMaps = RemoteCallUtil.callOfList(ssoURL + GET_AREA_LIST_URI, ImmutableMap.of("areaIds", id), null, HashMap.class, "SSO获取园区信息 通讯异常");
        }

        if(hashMaps == null){
            return babies;
        }

        for (BindedBabyVO baby : babies){

            for (Map<String, String> m : hashMaps){
                if(StringUtils.isNotEmpty(baby.getAreaId()) && baby.getAreaId().equals(m.get("id"))){
                    baby.setAreaName(m.get("areaName"));
                    break;
                }
            }

        }

        return babies;
    }

    private BindedBabyVO fillAreaForBaby(BindedBabyVO baby){
        String[] ids = {baby.getId()};
        String temp = Lists.newArrayList(ids).toString();
        String id = temp.substring(1, temp.length() - 1);

        List<HashMap> hashMaps = RemoteCallUtil.callOfList(ssoURL + GET_AREA_LIST_URI, ImmutableMap.of("areaId", id), null, HashMap.class, "SSO获取园区信息 通讯异常");

        if(hashMaps != null && hashMaps.size() > 0){
            HashMap m = hashMaps.get(0);

            baby.setAreaName((String) m.get("name"));
        }

        return baby;
    }

    @Override
    public BindedBabyVO getBindedBaby(String id, String mobile) {
        return babyNativeRepository.getBindedBaby(id, mobile);
    }

    @Override
    public List<BindedBabyVO> getAllBindedBabyEffective(String[] ids) {
        return babyNativeRepository.getAllBindedBabyEffective(ids);
    }
}
